﻿<UserControl x:Class="LINQPad.OpenAccess.UI.View.MsSqlConnectionSettingsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:conv="clr-namespace:LINQPad.OpenAccess.UI.Converters"
             xmlns:local="clr-namespace:LINQPad.OpenAccess.UI.View"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             MinHeight="41" MinWidth="250"
             mc:Ignorable="d" d:DesignWidth="396" Height="266">
    <UserControl.Resources>
        <conv:NegateBooleanConverter x:Key="negateBool" />
        <ControlTemplate x:Key="DatabaseSelector" TargetType="Control">
            <DockPanel>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="database:" />
                    <Image x:Name="LoadFailedIndicator" Grid.Row="0" Grid.Column="1"
                            Source="../Images/Error.png" Margin="2 1"
                            Height="12" Width="12" ToolTip="{Binding AvailableDatabasesLoadFailedMessage}" 
                            Visibility="Collapsed" />
                    <Button Grid.Row="0" Grid.Column="2" Margin="5 1"
                            Command="{Binding LoadAvailableDatabasesCommand}" >
                        <Image Source="../Images/Refresh.png" Height="12" Width="12" />
                    </Button>
                    <ComboBox Grid.Row="1" Grid.ColumnSpan="3" Margin="5 0" 
                              VerticalAlignment="Top" HorizontalAlignment="Stretch"
                              ItemsSource="{Binding AvailableDatabaseNames}" IsEditable="True"
                              Text="{Binding DatabaseName,Mode=TwoWay, ValidatesOnDataErrors=True}">
                    </ComboBox>
                </Grid>
            </DockPanel>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding AvailableDatabasesLoadFailed, FallbackValue=False}" Value="True">
                    <Setter TargetName="LoadFailedIndicator" Property="Visibility" Value="Visible" />
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        <ControlTemplate x:Key="SecuritySelector" TargetType="Control">
            <DockPanel>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto"  />
                        <RowDefinition Height="Auto"  />
                        <RowDefinition Height="Auto"  />
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Row="0" Grid.ColumnSpan="2" Text="authentication:" />
                    <RadioButton Grid.Row="1" Grid.Column="0" Margin="5 2"
                                IsChecked="{Binding UseWindowsAuthentication, Mode=TwoWay, FallbackValue=True}" Content="Windows" />
                    <RadioButton Grid.Row="1" Grid.Column="1" Margin="5 2"
                                IsChecked="{Binding UseWindowsAuthentication, Mode=TwoWay, Converter={StaticResource negateBool}}" Content="SQL Server" />

                    <TextBlock x:Name="UserNameHeader" Grid.Row="2" Grid.Column="0" Text="username:" Margin="5 0"
                               HorizontalAlignment="Right" VerticalAlignment="Center"
                               Visibility="Collapsed"/>
                    <TextBox x:Name="UserName" Grid.Row="2" Grid.Column="1" Style="{DynamicResource ValidatableTextBoxStyle}"
                             Text="{Binding UserName, Mode=TwoWay,ValidatesOnDataErrors=True}"
                             Visibility="Collapsed"/>
                    <TextBlock x:Name="PasswordHeader" Grid.Row="3" Grid.Column="0" Text="password:" 
                               Margin="5 0" HorizontalAlignment="Right" VerticalAlignment="Center"
                               Visibility="Collapsed"/>
                    <PasswordBox x:Name="Password" Grid.Row="3" Grid.Column="1" Style="{DynamicResource ValidatablePasswordBoxStyle}" VerticalAlignment="Top"
                                local:PasswordBoxHacks.Enabled="True"
                                local:PasswordBoxHacks.Password="{Binding Password, Mode=TwoWay,ValidatesOnDataErrors=True}"
                                Visibility="Collapsed"/>
                </Grid>
            </DockPanel>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding UseWindowsAuthentication, FallbackValue=True}" Value="False">
                    <Setter TargetName="UserNameHeader" Property="Visibility" Value="Visible" />
                    <Setter TargetName="UserName" Property="Visibility" Value="Visible" />
                    <Setter TargetName="PasswordHeader" Property="Visibility" Value="Visible" />
                    <Setter TargetName="Password" Property="Visibility" Value="Visible" />
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

    </UserControl.Resources>
    <DockPanel LastChildFill="False">
        <TextBlock DockPanel.Dock="Top" Text="server:" />
        <TextBox DockPanel.Dock="Top" Margin="5 1" Style="{DynamicResource ValidatableTextBoxStyle}"
                             Text="{Binding ServerName, Mode=TwoWay,ValidatesOnDataErrors=True}" />
        <Control DockPanel.Dock="Top"
                 Template="{StaticResource SecuritySelector}" />
        <Control DockPanel.Dock="Top"
                 Template="{StaticResource DatabaseSelector}" />
    </DockPanel>
</UserControl>
